package seckill;
import redis.clients.jedis.Jedis;
import java.util.UUID;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author 帅小伙
 * @date 2021/12/31
 * @description
 */
public class Seckill {

    // 商品101
    static{
        Jedis jedis = new Jedis("47.96.19.90", 23333);
        String storeKey = "sk:" + 101 + ":qt";
        jedis.set(storeKey,"100");
    }

    public static void main(String[] args) {

        SecKillRedis secKillRedis = new SecKillRedis();
        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(2,
                100,
                50,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(500),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy()
        );

        try{
            for (int i = 0; i < 200; i++) {
                poolExecutor.execute(()->{
                    secKillRedis.doSecKill(UUID.randomUUID().toString().substring(0,5),"101");
                });
            }
        }finally{
            poolExecutor.shutdown();
        }


    }

}
